import { judgeSing,MusicList,MusicFile, AlbumInfo, Album, Songs, ArtistData, Artist,
  ArtistInfo,
  ArtistSongs}from '../models/SongData'
import { requestGet } from '../utils/httpUtil'
import { promptAction } from '@kit.ArkUI';

//判断歌曲是否可用接口
export async function getJudgeSing(id:number,success:boolean,message:string): Promise<string>{
  let judge: judgeSing = new judgeSing();
  try {
    const response = await requestGet('/check/music?id='+id.toString());
    judge = JSON.parse(response);
    if (judge.code=='200') {
      message=judge.message;
    }
    return message;
  } catch (err) {
    // 错误处理：返回错误信息
    message = '错误代码为：' + (err.code || '未知错误') + ', 错误信息：' + JSON.stringify(err);
    // 弹出错误提示
    promptAction.showToast({ message: message });
    // 返回错误信息
    return message;
  }
}

//获取歌曲url接口
export async function getSingUrl(id:number,result:string): Promise<string>{
  let music: MusicList = new MusicList();
  let url:string='';
  try {
    const response = await requestGet('/song/url?id='+id.toString());
    music = JSON.parse(response);
    if (music) {
      const musicData = music.data[0];
      url=musicData.url;
    }
    return url;
  } catch (err) {
    // 错误处理：返回错误信息
    result = '错误代码为：' + (err.code || '未知错误') + ', 错误信息：' + JSON.stringify(err);
    // 弹出错误提示
    promptAction.showToast({ message: result });
    // 返回错误信息
    return result;
  }
}

//跳转函数
export async function goSing(id:number): Promise<string>{
  let result:string='';
  let success:boolean=false;
  const message = await getJudgeSing(id, success, result);
  if (message=='ok') {
    return getSingUrl(id,result);
  }else {
    return message;
  }
}

// 专辑信息获取
export async function getAlbum(
  id:number,
  hotSingerData: Album,
  result: string
): Promise<{ hotSingerData:Album } | string> {
  let hotSinger: AlbumInfo = new AlbumInfo();

  try {
    const response = await requestGet('/album?id='+id);
    hotSinger = JSON.parse(response);
    hotSingerData = null;
    if (hotSinger.code==200) {
      hotSingerData=hotSinger.album;
    }
    // 返回一个包含2个数组的对象
    return {
      hotSingerData: hotSingerData
    };
  } catch (err) {
    result = '错误代码为：' + err.code + ', 错误信息：' + JSON.stringify(err);
    promptAction.showToast({ message: result }); // 弹出错误提示
    console.error(result);
    return result;
  }
}

// 专辑歌曲信息获取
export async function getAlbumSinger(
  id:number,
  hotSingerData: Array<Songs>,
  result: string
): Promise<{ hotSingerData:Array<Songs>} | string> {
  let hotSinger: AlbumInfo = new AlbumInfo();

  try {
    const response = await requestGet('/album?id='+id);
    hotSinger = JSON.parse(response);
    hotSingerData = [];
    if (hotSinger.code==200) {
      hotSinger.songs.forEach((singer: Songs) => {
        if (singer.id) {
          hotSingerData.push(singer); // 将歌手添加到数组
        }
      });
    }
    // 返回一个包含2个数组的对象
    return {
      hotSingerData: hotSingerData
    };
  } catch (err) {
    result = '错误代码为：' + err.code + ', 错误信息：' + JSON.stringify(err);
    promptAction.showToast({ message: result }); // 弹出错误提示
    console.error(result);
    return result;
  }
}

//歌手信息获取
export async function getSinger(
  id:number,
  SingerData: Artist,
  result: string
): Promise<{ SingerData:Artist} | string> {
  let hotSinger: ArtistInfo = new ArtistInfo();

  try {
    const response = await requestGet('/artist/detail?id='+id);
    hotSinger = JSON.parse(response);
    SingerData = null;
    if (hotSinger.code==200) {
     SingerData = hotSinger.data.artist
    // 返回一个包含2个数组的对象
    return {
      SingerData: SingerData
     };
    }
  } catch (err) {
    result = '错误代码为：' + err.code + ', 错误信息：' + JSON.stringify(err);
    promptAction.showToast({ message: result }); // 弹出错误提示
    console.error(result);
    return result;
  }
}

// 歌手热门歌曲获取
export async function getHotSinger(
  id:number,
  hotSingerData: Array<Songs>,
  result: string
): Promise<{ hotSingerData:Array<Songs>} | string> {
  let hotSinger: ArtistSongs = new ArtistSongs();

  try {
    const response = await requestGet('/artists?id='+id);
    hotSinger = JSON.parse(response);
    hotSingerData = [];
    console.log(response)
    if (hotSinger.code==200) {
      hotSinger.hotSongs.forEach((singer: Songs) => {
        if (singer.id) {
          hotSingerData.push(singer); // 将歌手添加到数组
        }
      });
    }else {
      console.log('gg')
    }
    // 返回一个包含2个数组的对象
    return {
      hotSingerData: hotSingerData
    };
  } catch (err) {
    result = '错误代码为：' + err.code + ', 错误信息：' + JSON.stringify(err);
    promptAction.showToast({ message: result }); // 弹出错误提示
    console.error(result);
    return result;
  }
}